package org.jsola.hr.web;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyContractQuotaAddDTO;
import org.jsola.hr.dto.CompanyContractQuotaUpdateDTO;
import org.jsola.hr.query.CompanyContractQuotaQuery;
import org.jsola.hr.service.ICompanyContractQuotaService;
import org.jsola.hr.vo.CompanyContractQuotaVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;


/**
 * 公司合同配额
 *
 * @author zhr
 */
@Slf4j
@RestController("hrCompanyContractQuotaController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyContractQuotas")
public class CompanyContractQuotaController implements ICompanyContractQuotaControllerAPI {

    @Autowired
    private ICompanyContractQuotaService companyContractQuotaService;

    @Override
    @PostMapping
    public Result<CompanyContractQuotaVO> save(@Valid @RequestBody CompanyContractQuotaAddDTO companyContractQuotaAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增公司合同配额权限校验

        return Result.success(
                companyContractQuotaService.save(companyContractQuotaAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyContractQuotaUpdateDTO companyContractQuotaUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改公司合同配额权限校验

        int count = companyContractQuotaService.update(companyContractQuotaUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/{companyContractQuotaId}")
    public Result<Integer> delete(@PathVariable Long companyContractQuotaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除公司合同配额权限校验

        int count = companyContractQuotaService.deleteByIds(tokenUser, companyContractQuotaId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] companyContractQuotaIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除公司合同配额权限校验

        int count = companyContractQuotaService.deleteByIds(tokenUser, companyContractQuotaIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyContractQuotaId}")
    public Result<CompanyContractQuotaVO> findById(@PathVariable Long companyContractQuotaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司合同配额权限校验

        return Result.success(
                companyContractQuotaService.selectById(companyContractQuotaId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyContractQuotaVO>> listAll(@Valid CompanyContractQuotaQuery companyContractQuotaQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看公司合同配额权限校验

        return Result.success(
                companyContractQuotaService.select(companyContractQuotaQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyContractQuotaVO>> page(@Valid CompanyContractQuotaQuery companyContractQuotaQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyContractQuotaService.selectPage(companyContractQuotaQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 君子签回调接口
     */
    @PostMapping("/notify")
    public Result<Object> notifyCompany(HttpServletRequest request) throws IOException {
        // 从request中取得输入流
        InputStream inputStream = request.getInputStream();
        InputStreamReader inputReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        BufferedReader buffer = new BufferedReader(inputReader);
        StringBuilder reader = new StringBuilder();
        String line;
        while ((line = buffer.readLine()) != null) {
            reader.append(line);
        }
        Map<String, String> map = HttpUtil.decodeParamMap(reader.toString(), StandardCharsets.UTF_8);
        return Result.success(
                companyContractQuotaService.notify(map)
        );
    }

}

